package com.leetcode.leetcode.anyan;

public class _27移除元素 {

    public static int removeElement(int[] nums, int val) {
        int right = nums.length-1;
        int left = 0;
        while(left <= right) {//这里需要注意边界问题，当left == right的时候也要比较，不然会漏掉
            if (nums[left] == val) {
                nums[left] = nums[right];
                right--;
            } else {
                left++;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        int[] nums = {3, 2, 2, 2, 3};
        int target = 3;
        int len = removeElement(nums, target);
        for (int i = 0; i < len; i++ ) {
            System.out.println(i + "-" + nums[i]);
        }
//        System.out.println(removeElement(nums, target));
    }
}
